<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'cardd:rwjjmx:carddRwjjmx:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="100%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsCarddRwjjmxCarddRwjjmxForm">
  import { computed, ref, unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { CarddRwjjmx, carddRwjjmxForm, carddRwjjmxSave } from '/@/api/cardd/rwjjmx/carddRwjjmx';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('cardd.rwjjmx.carddRwjjmx');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<CarddRwjjmx>({} as CarddRwjjmx);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增任务奖金明细') : t('编辑任务奖金明细'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('日期'),
      field: 'rwjjmxRq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: true,
    },
    {
      label: t('驾驶员名称'),
      field: 'rwjjmxJsymc',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: true,
    },
    {
      label: t('车牌号'),
      field: 'rwjjmxCph',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: true,
    },
    {
      label: t('用车单位名称'),
      field: 'rwjjmxYcdwmc',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
      dynamicDisabled: true,
    },
    {
      label: t('车辆行程'),
      field: 'rwjjmxClxc',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
    },
    {
      label: t('上车日期'),
      field: 'rwjjmxKsrq',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
      dynamicDisabled: true,
    },
    {
      label: t('车单编号'),
      field: 'rw.ycddrwCdbh',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
      dynamicDisabled: true,
    },
    {
      label: '',
      field: 'detailInfo',
      component: 'Divider',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('小单单价'),
      field: 'rwjjmxXddj',
      component: 'InputNumber',
      // componentProps: {
      //   maxlength: 8,
      // },
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxXddj;
            const sl = formModel.rwjjmxXdsl;
            if (dj && sl) {
              formModel.rwjjmxXdxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxXdxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('小单数量'),
      field: 'rwjjmxXdsl',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxXddj;
            const sl = formModel.rwjjmxXdsl;
            if (dj && sl) {
              formModel.rwjjmxXdxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxXdxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('小单小计'),
      field: 'rwjjmxXdxj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('半天单价'),
      field: 'rwjjmxBtdj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          controls: false,
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const brzj = formModel.rwjjmxBtdj;
            const brzjsl = formModel.rwjjmxBtsl;
            if (brzj && brzjsl) {
              formModel.rwjjmxBtxj = (Number(brzj) * Number(brzjsl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxBtxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('半天数量'),
      field: 'rwjjmxBtsl',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          controls: false,
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const brzj = formModel.rwjjmxBtdj;
            const brzjsl = formModel.rwjjmxBtsl;
            if (brzj && brzjsl) {
              formModel.rwjjmxBtxj = (Number(brzj) * Number(brzjsl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxBtxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('半天小计'),
      field: 'rwjjmxBtxj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          controls: false,
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('全天单价'),
      field: 'rwjjmxQtdj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxQtdj;
            const sl = formModel.rwjjmxQtsl;
            if (dj && sl) {
              formModel.rwjjmxQtxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxQtxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('全天数量'),
      field: 'rwjjmxQtsl',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxQtdj;
            const sl = formModel.rwjjmxQtsl;
            if (dj && sl) {
              formModel.rwjjmxQtxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxQtxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('全天小计'),
      field: 'rwjjmxQtxj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超时单价'),
      field: 'rwjjmxCsdj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxCsdj;
            const sl = formModel.rwjjmxCssl;
            if (dj && sl) {
              formModel.rwjjmxCsxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxCsxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超时数量'),
      field: 'rwjjmxCssl',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxCsdj;
            const sl = formModel.rwjjmxCssl;
            if (dj && sl) {
              formModel.rwjjmxCsxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxCsxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超时小计'),
      field: 'rwjjmxCsxj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超公里单价'),
      field: 'rwjjmxCgldj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxCgldj;
            const sl = formModel.rwjjmxCglsl;
            if (dj && sl) {
              formModel.rwjjmxCglxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxCglxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超公里数量'),
      field: 'rwjjmxCglsl',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxCgldj;
            const sl = formModel.rwjjmxCglsl;
            if (dj && sl) {
              formModel.rwjjmxCglxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxCglxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超公里小计'),
      field: 'rwjjmxCglxj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('点单单价'),
      field: 'rwjjmxDddj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxDddj;
            const sl = formModel.rwjjmxDdsl;
            if (dj && sl) {
              formModel.rwjjmxDdxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxDdxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('点单数量'),
      field: 'rwjjmxDdsl',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            // 通过半日租金和半日租金数量计算半日租金金额
            const dj = formModel.rwjjmxDddj;
            const sl = formModel.rwjjmxDdsl;
            if (dj && sl) {
              formModel.rwjjmxDdxj = (Number(dj) * Number(sl)).toFixed(2);
              computHjje(formModel);
            } else {
              formModel.rwjjmxDdxj = '';
              computHjje(formModel);
            }
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('点单小计'),
      field: 'rwjjmxDdxj',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('停车费'),
      field: 'rwjjmxTcf',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('洗车费'),
      field: 'rwjjmxXcf',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('餐费'),
      field: 'rwjjmxCf',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('充电费'),
      field: 'rwjjmxCdf',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('其他'),
      field: 'rwjjmxQt',
      component: 'InputNumber',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 10,
          onInput: async () => {
            computHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('合计'),
      field: 'rwjjmxHj',
      component: 'InputNumber',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('备注'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 600,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 8, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await carddRwjjmxForm(data);
    record.value = (res.carddRwjjmx || {}) as CarddRwjjmx;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await carddRwjjmxSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  // 计算合计金额
  function computHjje(formModel) {
    let hjje = 0;
    hjje += Number(formModel.rwjjmxBtxj || 0);
    hjje += Number(formModel.rwjjmxXdxj || 0);
    hjje += Number(formModel.rwjjmxQtxj || 0);
    hjje += Number(formModel.rwjjmxCsxj || 0);
    hjje += Number(formModel.rwjjmxDdxj || 0);
    hjje += Number(formModel.rwjjmxTcf || 0);
    hjje += Number(formModel.rwjjmxXcf || 0);
    hjje += Number(formModel.rwjjmxCf || 0);
    hjje += Number(formModel.rwjjmxCdf || 0);
    hjje += Number(formModel.rwjjmxQt || 0);
    hjje += Number(formModel.rwjjmxCglxj || 0);
    formModel.rwjjmxHj = hjje.toFixed(2);
  }
</script>
